using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VTPUWC
    {
        public DataRow Convert2TPUWC(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryTPUWC(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["TPWMANDANT"] = " ".PadRight(2 ); 
         dwReturn["TPWTUID"] = " ".PadRight(8 ); 
         dwReturn["TPWEWLFDNR"] = " ".PadRight(0 ); 
         dwReturn["TPWRWLFDNR"] = " ".PadRight(0 ); 
         dwReturn["TPWWAKZ"] = " ".PadRight(1 ); 
         dwReturn["TPWKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["TPWWERK"] = " ".PadRight(3 ); 
         dwReturn["TPWABT"] = " ".PadRight(3 ); 
         dwReturn["TPWART"] = " ".PadRight(20 ); 
         dwReturn["TPWVAR"] = " ".PadRight(2 ); 
         dwReturn["TPWDPART"] = " ".PadRight(11 ); 
         dwReturn["TPWNETGEW"] = " ".PadRight(0 ); 
         dwReturn["TPWBSTIPKT"] = " ".PadRight(0 ); 
         dwReturn["TPWBSTKORR"] = " ".PadRight(0 ); 
         dwReturn["TPWAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["TPWAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["TPWAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["TPWLWDATE"] = " ".PadRight(0 ); 
         dwReturn["TPWLWTIME"] = " ".PadRight(0 ); 
         dwReturn["TPWBENID"] = " ".PadRight(8 ); 
         dwReturn["TPWTERM"] = " ".PadRight(8 ); 
         dwReturn["TPWFILLER"] = " ".PadRight(41 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapTPUWC(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveTPUWC(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryTPUWC(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapTPUWC(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertTPUWC(dwLocal, ITXDBVERSION);
            }
            UpdateTPUWC(dwLocal);
        }
        public void InsertTPUWC(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO TPUWC(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryTPUWC(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM TPUWC WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM TPUWC  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "TPUWC";
            Return = Result;
            return Return;
        }
        public void DeleteTPUWC(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM TPUWC   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateTPUWC(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE TPUWC SET "
                    +"TPWMANDANT= :TPWMANDANT,"
                    +"TPWTUID= :TPWTUID,"
                    +"TPWEWLFDNR= :TPWEWLFDNR,"
                    +"TPWRWLFDNR= :TPWRWLFDNR,"
                    +"TPWWAKZ= :TPWWAKZ,"
                    +"TPWKUNDNR= :TPWKUNDNR,"
                    +"TPWWERK= :TPWWERK,"
                    +"TPWABT= :TPWABT,"
                    +"TPWART= :TPWART,"
                    +"TPWVAR= :TPWVAR,"
                    +"TPWDPART= :TPWDPART,"
                    +"TPWNETGEW= :TPWNETGEW,"
                    +"TPWBSTIPKT= :TPWBSTIPKT,"
                    +"TPWBSTKORR= :TPWBSTKORR,"
                    +"TPWAUFKRS= :TPWAUFKRS,"
                    +"TPWAUFLFD= :TPWAUFLFD,"
                    +"TPWAUFPOS= :TPWAUFPOS,"
                    +"TPWLWDATE= :TPWLWDATE,"
                    +"TPWLWTIME= :TPWLWTIME,"
                    +"TPWBENID= :TPWBENID,"
                    +"TPWTERM= :TPWTERM,"
                    +"TPWFILLER= :TPWFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("TPWMANDANT", OracleType.NVarChar).Value = dwData["TPWMANDANT"].ToString(); 
          or1.Parameters.Add("TPWTUID", OracleType.NVarChar).Value = dwData["TPWTUID"].ToString(); 
          or1.Parameters.Add("TPWEWLFDNR", OracleType.NVarChar).Value = dwData["TPWEWLFDNR"].ToString(); 
          or1.Parameters.Add("TPWRWLFDNR", OracleType.NVarChar).Value = dwData["TPWRWLFDNR"].ToString(); 
          or1.Parameters.Add("TPWWAKZ", OracleType.NVarChar).Value = dwData["TPWWAKZ"].ToString(); 
          or1.Parameters.Add("TPWKUNDNR", OracleType.NVarChar).Value = dwData["TPWKUNDNR"].ToString(); 
          or1.Parameters.Add("TPWWERK", OracleType.NVarChar).Value = dwData["TPWWERK"].ToString(); 
          or1.Parameters.Add("TPWABT", OracleType.NVarChar).Value = dwData["TPWABT"].ToString(); 
          or1.Parameters.Add("TPWART", OracleType.NVarChar).Value = dwData["TPWART"].ToString(); 
          or1.Parameters.Add("TPWVAR", OracleType.NVarChar).Value = dwData["TPWVAR"].ToString(); 
          or1.Parameters.Add("TPWDPART", OracleType.NVarChar).Value = dwData["TPWDPART"].ToString(); 
          or1.Parameters.Add("TPWNETGEW", OracleType.NVarChar).Value = dwData["TPWNETGEW"].ToString(); 
          or1.Parameters.Add("TPWBSTIPKT", OracleType.NVarChar).Value = dwData["TPWBSTIPKT"].ToString(); 
          or1.Parameters.Add("TPWBSTKORR", OracleType.NVarChar).Value = dwData["TPWBSTKORR"].ToString(); 
          or1.Parameters.Add("TPWAUFKRS", OracleType.NVarChar).Value = dwData["TPWAUFKRS"].ToString(); 
          or1.Parameters.Add("TPWAUFLFD", OracleType.NVarChar).Value = dwData["TPWAUFLFD"].ToString(); 
          or1.Parameters.Add("TPWAUFPOS", OracleType.NVarChar).Value = dwData["TPWAUFPOS"].ToString(); 
          or1.Parameters.Add("TPWLWDATE", OracleType.NVarChar).Value = dwData["TPWLWDATE"].ToString(); 
          or1.Parameters.Add("TPWLWTIME", OracleType.NVarChar).Value = dwData["TPWLWTIME"].ToString(); 
          or1.Parameters.Add("TPWBENID", OracleType.NVarChar).Value = dwData["TPWBENID"].ToString(); 
          or1.Parameters.Add("TPWTERM", OracleType.NVarChar).Value = dwData["TPWTERM"].ToString(); 
          or1.Parameters.Add("TPWFILLER", OracleType.NVarChar).Value = dwData["TPWFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
